//容器，拼版区域，矩形
var container = {}
//已经布置到拼版上的卡片
var placedCards = []


//根据已经布置的卡片，查找剩下的空白区域。
function findSpace(rectList, container) {
	for (var i = 0; i < rectList; i++) {
		var rect = rectList[i];
	}
}

function mergeBigArea(rectList) {
	if (!rectList || !rectList.length) return;
	var left = 0;

	for (i = 0; i < rectList.length; i++) {
		var rect = rectList[i];
		if (left == 0 || left > rect.x) {
			left = rect.x;
		}
	}

}

function place(rect) {
	if (!rect) return;
	placedCards.push(rect)
}

function init(cntr, cards) {
	console.log('makeup.init', cntr, cards)
	container = cntr || {
		width: 464,
		height: 320
	};
	placedCards = cards || placedCards;
}


function each(list, handler) {
	for (var i = 0; i < list.length; i++) {
		handler(list[i], i);
	}
}


function anyMatch(list, predicate) {
	for (var i = 0; i < list.length; i++) {
		if (predicate(list[i])) return true;
	}
	return false;
}

function noneMatch(list, predicate) {
	for (var i = 0; i < list.length; i++) {
		if (predicate(list[i])) return false;
	}
	return true;
}

//判断某个矩形是否被一些矩形给包围（定点被围，边上的空白不算）
function isRectLive(rect, rectList, cntr = container) {

	//var rectList = removeIf()
	var lsTop = rectList.filter((r) => {
		return r != rect && (r.y + r.height) == rect.y && (r.x + r.width >= rect.x && r.x <= rect.x + rect
			.width);
	});
	var lsBottom = rectList.filter((r) => {
		return r != rect && (r.y) == rect.y + rect.height && (r.x + r.width >= rect.x && r.x <= rect.x + rect
			.width);
	});

	var lsLeft = rectList.filter((r) => {
		return r != rect && (r.x + r.width) == rect.x && (r.y + r.height >= rect.y && r.y <= rect.y + rect
			.height);
	});
	var lsRight = rectList.filter((r) => {
		return r != rect && (r.x) == rect.x + rect.width && (r.y + r.height >= rect.y && r.y <= rect.y + rect
			.height);
	});
	//console.log(rect, lsTop, lsBottom, lsLeft, lsRight)

	var closedLT = (rect.y == 0 || anyMatch(lsTop, (r) => {
		return r.x <= rect.x
	})) && (rect.x == 0 || anyMatch(lsLeft, (r) => {
		return r.y <= rect.y
	}));
	if (!closedLT) return true;

	var closedRT = (rect.y == 0 || anyMatch(lsTop, (r) => {
		return r.x + r.width >= rect.x + rect.width
	})) && (rect.x + rect.width == cntr.width || anyMatch(lsRight, (r) => {
		return r.y <= rect.y
	}));
	if (!closedRT) return true;

	var closedLB = (rect.y + rect.height == cntr.height || anyMatch(lsBottom, (r) => {
		return r.x <= rect.x
	})) && (rect.x == 0 || anyMatch(lsLeft, (r) => {
		return r.y + r.height >= rect.y + rect.height
	}));
	if (!closedLB) return true;

	var closedRB = (rect.y + rect.height == cntr.height || anyMatch(lsBottom, (r) => {
		return r.x + r.width >= rect.x + rect.width
	})) && (rect.x + rect.width == cntr.width || anyMatch(lsRight, (r) => {
		return r.y + r.height >= rect.y + rect.height
	}));
	if (!closedRB) return true;

	//console.log(rect, closedLT, closedRT, closedLB, closedRB)
	return false;

}

//查找出卡片中没有开放顶点。
function livePointList(rect, rectList, cntr = container) {

	//var rectList = removeIf()
	var lsTop = rectList.filter((r) => {
		return r != rect && (r.y + r.height) == rect.y && (r.x + r.width >= rect.x && r.x <= rect.x + rect
			.width);
	});
	var lsBottom = rectList.filter((r) => {
		return r != rect && (r.y) == rect.y + rect.height && (r.x + r.width >= rect.x && r.x <= rect.x + rect
			.width);
	});

	var lsLeft = rectList.filter((r) => {
		return r != rect && (r.x + r.width) == rect.x && (r.y + r.height >= rect.y && r.y <= rect.y + rect
			.height);
	});
	var lsRight = rectList.filter((r) => {
		return r != rect && (r.x) == rect.x + rect.width && (r.y + r.height >= rect.y && r.y <= rect.y + rect
			.height);
	});
	//console.log(rect, lsTop, lsBottom, lsLeft, lsRight)

	var livePoints = [];

	var closedLT = (rect.y == 0 || anyMatch(lsTop, (r) => {
		return r.x <= rect.x
	})) && (rect.x == 0 || anyMatch(lsLeft, (r) => {
		return r.y <= rect.y
	}));
	if (!closedLT) {
		livePoints.push({
			x: rect.x,
			y: rect.y,
			type: "LT",
			rect: rect
		});
	}

	var closedRT = (rect.y == 0 || anyMatch(lsTop, (r) => {
		return r.x + r.width >= rect.x + rect.width
	})) && (rect.x + rect.width == cntr.width || anyMatch(lsRight, (r) => {
		return r.y <= rect.y
	}));
	if (!closedRT) {
		livePoints.push({
			x: rect.x + rect.width,
			y: rect.y,
			type: "RT",
			rect: rect
		});
	};

	var closedLB = (rect.y + rect.height == cntr.height || anyMatch(lsBottom, (r) => {
		return r.x <= rect.x
	})) && (rect.x == 0 || anyMatch(lsLeft, (r) => {
		return r.y + r.height >= rect.y + rect.height
	}));
	if (!closedLB) {
		livePoints.push({
			x: rect.x,
			y: rect.y + rect.height,
			type: "LB",
			rect: rect
		});
	};

	var closedRB = (rect.y + rect.height == cntr.height || anyMatch(lsBottom, (r) => {
		return r.x + r.width >= rect.x + rect.width
	})) && (rect.x + rect.width == cntr.width || anyMatch(lsRight, (r) => {
		return r.y + r.height >= rect.y + rect.height
	}));
	if (!closedRB) {
		livePoints.push({
			x: rect.x + rect.width,
			y: rect.y + rect.height,
			type: "RB",
			rect: rect
		});
	};

	//console.log(rect, closedLT, closedRT, closedLB, closedRB)
	return livePoints;

}
//查找所有四周还有空白区域的卡片。
function findLiveRects(rectList = placedCards, cntr = container) {
	//console.log(rectList, cntr)
	return rectList.filter((x) => {
		return isRectLive(x, rectList, cntr);
	})
}
//查找所有四周还有空白区域的卡片。
function findLivePoints(rectList = placedCards, cntr = container) {
	//console.log(rectList, cntr)
	var points = [];
	each(rectList, function(rect, i) {
		var ps = livePointList(rect, rectList)
		each(ps, function(p) {
			points.push(p)
		})
	});
	return points;
}


//找到(x,y)定位的卡片。
function locateRect(x, y, rectList = placedCards) {
	//console.log(rectList, cntr)
	var ls = rectList.filter((r) => {
		return x >= r.x && x <= r.x + r.width && y >= r.y && y <= r.y + r.height;
	});
	return ls.length > 0 ? ls[0] : null;
}
//根据
function buildShapeList(liveRectList) {

}
function hello(){
	console.log(container, placedCards)
}
function bye(){
	console.log("bye")
}
export default {
	hello,bye,jjj:function(){}
	// hello: function() {
	// 	console.log(container, placedCards)
	// },
	// placedCards: function() {
	// 	return placedCards;
	// },
	// place: place,
	// init: init,
	// findLiveRects: findLiveRects,
	// isRectLive: isRectLive,
	// locateRect: locateRect,
	// findLivePoints: findLivePoints
}

function getPlacedCards() {
	return placedCards;
}
export {
	getPlacedCards as placedCards,
	place,
	init,
	each,
	findLiveRects,
	isRectLive,
	locateRect,
	findLivePoints
}